<?php

namespace app\admin\model;

use think\Model;
use think\model\concern\SoftDelete;

class Permission extends Model
{
    use SoftDelete;
    protected $deleteTime = 'delete_time';
    protected $autoWriteTimestamp = 'datetime';

    const PAGESIZE = 15;

    public static function buildData(){
        $data = self::where('parent_id',0)->field('id,name,route,level')->select();
        foreach($data as $k=>$v){
            $data[$k]['child'] = self::where('parent_id',$v['id'])->field('id,name,route,level')->select();
        }
        return $data;
    }

    public static function getLevel($id){
        $level = self::where('id',$id)->value('level');
        return $level + 1;
    }

    public static function getTreeView(){
        $data = self::where('parent_id',0)->field('id,name,route,level')->select();
        foreach($data as $k=>$v){
            $data[$k]['child'] = self::where('parent_id',$v['id'])->field('id,name,route,level')->select();
            foreach($data[$k]['child'] as $kk=>$vv){
                $data[$k]['child'][$kk]['sec_child'] = self::where('parent_id',$vv['id'])->field('id,name,route,level')->select();
            }
        }
        return $data;
    }
}
